package casa.depesos.app.com.utils;

import android.content.Intent;

import java.util.ArrayList;

import casa.depesos.app.com.App;
import casa.depesos.app.com.activity.BankActivity;
import casa.depesos.app.com.activity.CardActivity;
import casa.depesos.app.com.activity.FaceActivity;
import casa.depesos.app.com.activity.InfoActivity;
import casa.depesos.app.com.activity.TouchActivity;
import casa.depesos.app.com.activity.WorkActivity;
import casa.depesos.app.com.http.ReqCallback;
import casa.depesos.app.com.http.bean.LevelBean;
import casa.depesos.app.com.http.bean.ProgressBean;
import casa.depesos.app.com.http.req.LevelReq;
import casa.depesos.app.com.http.req.ProgressReq;

public class CommonJump {

    final ArrayList<String> amount = new ArrayList<>();

    private void getLevel(Runnable runnable) {
        if (!amount.isEmpty()) {
            runnable.run();
            return;
        }
        new LevelReq().req(new ReqCallback<LevelBean>() {
            @Override
            public void callback(boolean success, LevelBean bean) {
                if (success && bean.isValid()) {
                    amount.clear();
                    amount.addAll(bean.list);
                    if (runnable != null) {
                        runnable.run();
                    }
                }
            }
        });
    }

    public void getData() {
        new ProgressReq().req(new ReqCallback<ProgressBean>() {
            @Override
            public void callback(boolean success, ProgressBean bean) {
                if (success && bean.isValid()) {
                    jump(bean.data.status, true);
                }
            }
        });
    }

    public void jump(int status, boolean auto) {
        switch (status) {
            case 0:
                if (!auto) {
                    getData();
                }
                break;
            case 10: {
                getLevel(new Runnable() {
                    @Override
                    public void run() {
                        Intent i = new Intent(App.getApp(), InfoActivity.class);
                        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        i.putStringArrayListExtra("amount", amount);
                        App.getApp().startActivity(i);
                    }
                });
                break;
            }
            case 20: {
                getLevel(new Runnable() {
                    @Override
                    public void run() {
                        Intent i = new Intent(App.getApp(), WorkActivity.class);
                        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        i.putStringArrayListExtra("amount", amount);
                        App.getApp().startActivity(i);
                    }
                });
                break;
            }
            case 25: {
                getLevel(new Runnable() {
                    @Override
                    public void run() {
                        Intent i = new Intent(App.getApp(), TouchActivity.class);
                        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        i.putStringArrayListExtra("amount", amount);
                        App.getApp().startActivity(i);
                    }
                });
                break;
            }
            case 30: {
                Intent i = new Intent(App.getApp(), CardActivity.class);
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                App.getApp().startActivity(i);
                break;
            }
            case 40: {
                Intent i = new Intent(App.getApp(), FaceActivity.class);
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                App.getApp().startActivity(i);
                break;
            }
            case 50: {
                Intent i = new Intent(App.getApp(), BankActivity.class);
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                App.getApp().startActivity(i);
                break;
            }
        }
    }
}
